<template>
  <basic-card
    :title="$t('menu.push_broadcast')"
    :iconClass="'iconfont icon-broadcast'"
  >
    <el-form label-position="top" label-width="120px" :model="formData" :rules="formRule" v-loading="loading" style="margin-top: 30px;">
      <el-row :gutter="60" style="width: 720px; margin-left: auto; margin-right: auto;">
        <el-col :span="12">
          <el-form-item :label="$t('broadcast.device_name')" prop="name">
            <basic-input v-model="formData.name"></basic-input>
          </el-form-item>
          <el-form-item :label="$t('broadcast.broadcast_coding')" prop="broadcast_coding">
            <basic-input v-model="formData.broadcast_coding"></basic-input>
          </el-form-item>
          <el-form-item :label="$t('broadcast.devices_type')" prop="devices_type">
            <basic-select v-model="formData.devices_type" style="width: 100%;">
              <el-option
                v-for="item in devicesTypeSelectData"
                :key="item.id"
                :label="item.label"
                :value="item.id">
                {{ item.label }}
              </el-option>
            </basic-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('broadcast.devices_brands')" prop="devices_brands">
            <basic-select v-model="formData.devices_brands" style="width: 100%;">
              <el-option
                v-for="item in brandSelectData"
                :key="item.id"
                :label="item.name"
                :value="item.id">
              </el-option>
            </basic-select>
          </el-form-item>
          <el-form-item :label="$t('broadcast.devices_model')" prop="devices_model">
            <basic-select v-model="formData.devices_model" style="width: 100%;">
              <el-option
                v-for="item in devicesModelSelectData"
                :key="item.id"
                :label="item.name"
                :value="item.id">
              </el-option>
            </basic-select>
          </el-form-item>
          <el-form-item :label="$t('broadcast.position')" prop="position">
            <basic-input v-model="formData.position"></basic-input>
          </el-form-item>
        </el-col>
        <el-col :span="24" style="margin-top: 30px; text-align: center;">
          <!-- <el-button @click="close">{{ $t("botton.cancel") }}</el-button> -->
          <el-button type="primary" @click="submit" :disabled="loading">{{ $t("botton.save") }}</el-button>
        </el-col>
      </el-row>
    </el-form>
  </basic-card>
</template>
<script lang="ts">
import Vue from 'vue';
import BasicCard from "@/components/Basic/Card/BasicCard.vue";
import BasicInput from "@/components/Basic/Form/BasicInput.vue";
import BasicSelect from "@/components/Basic/Form/BasicSelect.vue";

export default Vue.extend({
  components: { BasicCard, BasicInput, BasicSelect },
  data() {
    return {
      loading: false,
      devicesModelSelectData: [],
      brandSelectData: [],
      devicesTypeSelectData: [
        {
          id: this.$config.BROADCAST_DEVICE_TYPE.microphone,
          label: this.$t("broadcast.microphone")
        },
        {
          id: this.$config.BROADCAST_DEVICE_TYPE.audioCollector,
          label: this.$t("broadcast.audioCollector")
        },
        {
          id: this.$config.BROADCAST_DEVICE_TYPE.fireControlCollector,
          label: this.$t("broadcast.fireControlCollector")
        },
        {
          id: this.$config.BROADCAST_DEVICE_TYPE.networkAudio,
          label: this.$t("broadcast.networkAudio")
        },
        {
          id: this.$config.BROADCAST_DEVICE_TYPE.networkPowerAmplifier,
          label: this.$t("broadcast.networkPowerAmplifier")
        }
      ],
      formData: {
        name: "",
        devices_model: "",
        devices_brands: "",
        broadcast_coding: "",
        devices_type: "",
        position: ""
      },
      formRule: {
        name: [{ required: true, message: this.$t("broadcast.device_name_required_massage"), trigger: 'blur' }],
        devices_model: [{ required: true, message: this.$t("broadcast.devices_model_required_message"), trigger: 'blur' }],
        devices_brands: [{ required: true, message: this.$t("broadcast.devices_brands_required_message"), trigger: 'blur' }],
        broadcast_coding: [{ required: true, message: this.$t("broadcast.broadcast_coding_required_message"), trigger: 'blur' }],
        devices_type: [{ required: true, message: this.$t("broadcast.devices_type_required_message"), trigger: 'blur' }],
      }
    }
  },
  async mounted() {
    this.loading = true;
    try {
      await this.initDevicesModelSelectData();
      await this.initBrandSelectData();
    } catch (error) {
      this.$message.warning(`${this.$t("broadcast.form_init_fail_message")}`);
    }
    this.loading = false;
  },
  methods: {
    submit() {
      this.$mainApi.broadcastApi.postBroadcast({
        "brandId": parseInt(this.formData.devices_brands),
        "broadcastCode": this.formData.broadcast_coding,
        "broadcastIpAddress": this.formData.position,
        "broadcastName": this.formData.name,
        "type": parseInt(this.formData.devices_type),
        "deviceModelId": parseInt(this.formData.devices_model)
      }).then(() => {
        this.$router.push("/broadcast/list");
      });;
    },
    async initDevicesModelSelectData() {
      const result = await this.$mainApi.devicesModelApi.getDevicesModelByPage({
        page: 1,
        page_size: 9999
      });
      this.devicesModelSelectData = (result as any).data.list;
    },
    async initBrandSelectData() {
      const result = await this.$mainApi.brandsApi.getBrandsByPage({
        page: 1,
        page_size: 9999
      });
      this.brandSelectData = (result as any).data.list
    },
    close() {

    }
  }
})
</script>
<style lang="scss" scoped>
.image-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.image-uploader .el-upload:hover {
  border-color: #409eff;
}
.image-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}
.image-uploader-image {
  width: 178px;
  height: 178px;
  display: block;
}
</style>
